import logging
import os
import datetime

logDir_path = r"D:\project\Product Development Platform\operation_log"


def get_log():
    """
        将日志输出到控制台和文件中
    :param :
    :return: 日志对象
    """
    # 创建logger实例
    logger = logging.getLogger()
    # 设置日志级别 设置的日志级别以这个为准
    logger.setLevel(logging.INFO)
    # 创建控制台、文件处理器，将日志输出到控制台
    console_handler = logging.StreamHandler()
    # all_dir = os.listdir(logDir_path)
    # log_time = datetime.datetime.now().strftime("%Y-%m-%d %H：%M：%S")
    # log_path = os.path.join(logDir_path, f"{log_time}.txt")
    file_handle = logging.FileHandler("1.txt", "w", encoding="utf-8")
    # 创建日志格式
    formatter = logging.Formatter('%(asctime)s - %(module)s - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S")
    # 将日志格式应用到处理器
    console_handler.setFormatter(formatter)
    file_handle.setFormatter(formatter)
    # 将处理器添加到logger实例中
    logger.addHandler(console_handler)
    logger.addHandler(file_handle)
    return logger

    # """
    #     这样写无法将日志信息输出到控制台
    # """
    # logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(levelname)s - %(message)s'
    #                     , filename="../log.txt", filemode="a",datefmt="%Y-%m-%d %H:%M:%S")
    # return logging.getLogger()


log = get_log()
